<?php

namespace App\Http\Controllers\Trade;

use App\Exceptions\UserRequestException;
use App\Library\Mall\Seller\SoldManagers;
use App\Models\Express;
use App\Models\Order;
use Illuminate\Http\Request;

class SoldController extends BaseController
{
    use SoldManagers;

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws UserRequestException
     */
    public function detail(Request $request)
    {
        $step = 'auction';
        $order_id = $request->input('order_id');
        $order = $this->user()->solds()->with(['items', 'shipping', 'transaction'])->find($order_id);
        if ($order) {
            if ($order->trade_state == 2) {
                $step = 'pay';
            } elseif ($order->trade_state == 3) {
                $step = 'send';
            } elseif ($order->trade_state == 4 && $order->buyer_rate == 0) {
                $step = 'confirm';
            } elseif ($order->trade_state == 4 && $order->buyer_rate == 1) {
                $step = 'rate';
            }

            $expresses = Express::all();
            return $this->view('trade.sold.detail', compact('step', 'order_id', 'order', 'expresses'));
        } else {
            throw new UserRequestException(trans('order.order does not exist'), 404);
        }
    }

    /**
     * @param Request $request
     * @param Order $order
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function sendOrderResponse(Request $request, Order $order)
    {
        return $this->messager()
            ->setMessage(__('order.delivery success'))
            ->setRedirectTo('trade/sold/detail?order_id='.$order->order_id)->render();
    }
}
